<?php
namespace app\mobile\controller;
use app\common\controller\Base;
use think\cache\driver\Redis;
use think\Db;

class Index extends Base
{
// 	protected $beforeActionList = [
//         '_if_login' =>  ['except'=>'down,iosJc,iosjc'],
//     ];
    
//     /**未登录情况下需要做的处理**/
//     protected function _if_login()
//     {
//     	/*
//   		dump($request->controller());dump($request->module());dump($request->action());
//   		*/
//     	if($this->user_all['id'] <= 0)  //手机端未登录的情况下
//         {
//         	$this->redirect('index/down');
//         	exit;
//         }
//     }

    public function _initialize() {
        parent::_initialize();
        $start = array(
	            'user_all'      =>$this->user_all,
	            'app_down' =>$this->app_down,
	            'logo'           =>$this->logo
	    );

        $this->assign($start);

        $request=  \think\Request::instance();
    }

    /**
     ** 赢彩网手机端首页
    **/
    public function down(){
		$iosObj = Db::name('version')->where(['type'=>2,'systemtype'=>2])->order('number DESC')->find();
		$androidObj = Db::name('version')->where(['type'=>2,'systemtype'=>1])->order('number DESC')->find();
		$ios = $android = '';
		if( !empty( $iosObj ) ){
			$ios = 'itms-services://?action=download-manifest&url='.$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/ios/'.$iosObj['version_url'];
		}
		if( !empty( $androidObj ) ){
			$android = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/apk/'.$androidObj['version_url'];
		}
		$this->assign('ios',$ios);
		$this->assign('android',$android);
		return $this->fetch();
    }

    /** ios 安装教程 **/
    public function iosJc()
    {
    	$ios_jcimg=Db::name('xt_app_jc')->find();
    	$this->assign('ios_jcimg',$ios_jcimg);
    	return $this->fetch();
    }
	
	 public function mhome()
    {
		$iosObj = Db::name('version')->where(['type'=>2,'systemtype'=>2])->order('number DESC')->find();
		$androidObj = Db::name('version')->where(['type'=>2,'systemtype'=>1])->order('number DESC')->find();
		$ios = $android = '';
		if( !empty( $iosObj ) ){
			$ios = 'itms-services://?action=download-manifest&url=https://6709a.com/ios/'.$iosObj['version_url'];
		}          
		if( !empty( $androidObj ) ){
			$android = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/apk/'.$androidObj['version_url'];
		}
		$this->assign('ios',$ios);
		$this->assign('android',$android);

		return  $this->fetch('mhome');
    }

    //手机版首页入口
    public function mhome1()
    {
        $sign = Db::name('xt_gonggao')->where('suggest_mobile',1)->count();
    	$pc_guangg =  Db::name('sjgg')->where('gg_class<5')->select();  //手机广告
    	$rem_top= Db::name('caipai_list')->where('cp_if_open<2')->order('cp_mb_px desc')->select();	 //要展示的彩种
        $hdlbtest = Db::name('xt_gonggao')->where('xianshi',1)->order('paixu','DESC')->select();//滚动公告
        $zxkf = Db::name('xt_kflink')->where('type',4)->field('qq')->find();
         
         
        //首页中奖排行榜
		$reward_list = db('touzhu_list_ok')->alias('tlo')
		->field('tlo.username,tlo.zjje,cl.cp_name_old')
		->join('__CAIPAI_LIST__ cl','cl.id=tlo.czid')
		->join('__USERNAME__ u','u.username=tlo.username')
		->where('u.swzh&u.nbzh','<>','1')
		->where('tlo.zjje','>',0)
		->order('tlo.tz_time','DESC')
		->limit(30)
		->select();
		/* for($i=1;$i<=(100-count($reward_list));$i++) {
			$usern = suiji_zifu(4);
			$zhongjje = rand(10000,1000000);
			$zhongjje = $zhongjje / 100;
			$phblist[$i]['usern'] = $usern;
			$phblist[$i]['zhongjje'] = sprintf('%.2f',$zhongjje);
			$phblist[$i]['cp_name'] = '重庆时时彩';
			$phblist[$i]['id'] = $i;
		} */
		$reward_count = count($reward_list) + 1;
		foreach($reward_list as $k=>$v) {
		    $phblist[$reward_count+$k]['usern'] = substr($v['username'],0,4);
			$phblist[$reward_count+$k]['zhongjje'] = sprintf('%.2f',$v['zjje']);
			$phblist[$reward_count+$k]['cp_name'] = $v['cp_name_old'];
		}
		//打乱数组
		//shuffle($phblist);

		//查询红包是否开启
        $bonus = Db::name('bonus_lay')->field('flip')->find();

        foreach($rem_top as & $rem_single)
		{
			if($rem_single['cp_name_new']=="")
			{
				$rem_single['cp_name_new']=$rem_single['cp_name_old'];
			}
		}
		$f = Db::name('username')->where('username',$this->user_all['username'])->find();
		$this->assign('zxkf',$zxkf['qq']);
		$this->assign('f',$f);
		$this->assign('pc_guangg',$pc_guangg);
		$this->assign('sign',$sign);
        $this->assign('bonus', $bonus);
		$this->assign('rem_top',$rem_top);
		$this->assign('phblist',$phblist);
		$this->assign('lblist',$hdlbtest);
		$this->assign('phtime',date('Y-m-d'));

		return  $this->fetch('index');
    }
    public function mhomeAndroid()
    {
    	$pc_guangg =  Db::name('sjgg')->where('gg_class<5')->select();  //手机广告
    	$rem_top= Db::name('caipai_list')->where('cp_if_open<2')->order('cp_mb_px desc')->select();	 //要展示的彩种
    	$hdlbtest = Db::name('xt_gonggao')->where('xianshi',1)->order('paixu','DESC')->select();//滚动公告
    
    	//首页中奖排行榜
    	$reward_list = db('touzhu_list_ok')->alias('tlo')
    	->field('tlo.username,tlo.zjje,cl.cp_name_old')
    	->join('__CAIPAI_LIST__ cl','cl.id=tlo.czid')
    	->join('__USERNAME__ u','u.username=tlo.username')
    	->where('u.swzh&u.nbzh','<>','1')
    	->where('tlo.zjje','>',0)
    	->order('tlo.zjje','DESC')
    	->limit(99)
    	->select();
    	for($i=1;$i<=(100-count($reward_list));$i++) {
    		$usern = suiji_zifu(4);
    		$zhongjje = rand(10000,1000000);
    		$zhongjje = $zhongjje / 100;
    		$phblist[$i]['usern'] = $usern;
    		$phblist[$i]['zhongjje'] = sprintf('%.2f',$zhongjje);
    		$phblist[$i]['cp_name'] = '重庆时时彩';
    		$phblist[$i]['id'] = $i;
    	}
    	$reward_count = count($phblist) + 1;
    	foreach($reward_list as $k=>$v) {
    		$phblist[$reward_count+$k]['usern'] = substr($v['username'],0,4);
    		$phblist[$reward_count+$k]['zhongjje'] = sprintf('%.2f',$v['zjje']);
    		$phblist[$reward_count+$k]['cp_name'] = $v['cp_name_old'];
    	}
    	//打乱数组
    	shuffle($phblist);
    
    	foreach($rem_top as & $rem_single)
    	{
    		if($rem_single['cp_name_new']=="")
    		{
    			$rem_single['cp_name_new']=$rem_single['cp_name_old'];
    		}
    	}
    	$f = Db::name('username')->where('username',$this->user_all['username'])->find();
    	$this->assign('f',$f);
    	$this->assign('pc_guangg',$pc_guangg);
    	$this->assign('rem_top',$rem_top);
    	$this->assign('phblist',$phblist);
    	$this->assign('lblist',$hdlbtest);
    	$this->assign('phtime',date('Y-m-d'));
    
    	return  $this->fetch('index2');
    }
    
    public function sign(){
//	    $redis = new Redis();
	    
//	    $list = $redis->get('sign');
//	    if(!$list){
	       
	        $lists = Db::name('xt_gonggao')->where('suggest_web',1)->field('id,title,ggtext')->order('fatime desc')->select();
//	        $redis->set('sign',json_encode($lists,JSON_UNESCAPED_UNICODE),300);
//	        $list = $redis->get('sign');
//	    }
	    $this->assign('list',$lists);
	    return $this->fetch();
	}
	
	public function course(){
		return $this->fetch();
	}
}
